﻿/*************************************************************************************
 * 文 件 名:   HK_CircleFramesAnimator
 * 
 * 描    述:   UI 循环动画序列帧播放控制脚本
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2022/7/30
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

namespace HKTools
{
    [RequireComponent(typeof(Image))] // 自动添加Image组件
    public class HK_CircleFramesAnimator : MonoBehaviour
    {
        [Header("是否自动播放："), SerializeField] bool isAutoPlay = false;
        [Header("循环停顿时间："), SerializeField] float loopPauseTime;
        [Header("动画帧率："), SerializeField] float frameRate = 25.0f;

        [Header("初始序列帧："), SerializeField] List<Sprite> inFrame_List;
        [Header("循环序列帧："), SerializeField] List<Sprite> circleFrame_List;
        [Header("结束序列帧："), SerializeField] List<Sprite> outFrame_List;

        // 当前的序列帧动画
        List<Sprite> curFrame_List = new List<Sprite>();

        enum AnimType
        {
            In,
            Circle,
            Out
        }

        AnimType animType;

        Image image;

        bool isPlaying;
        bool IsPlaying
        {
            get { return isPlaying; }
            set
            {
                isPlaying = value;
                image.enabled = value;
            }
        }

        int frameIndex = 0;
        float time;

        void Awake()
        {
            image = GetComponent<Image>();

            if (inFrame_List.Count > 0)
            {
                image.sprite = inFrame_List[frameIndex];
            }
            else
            {
                if (circleFrame_List.Count > 0)
                {
                    image.sprite = circleFrame_List[frameIndex];
                }
                else
                {
                    Debug.LogError("没有添加循环序列帧");
                }
            }

            IsPlaying = false;

            if (isAutoPlay)
                PlayFramesAnim();
        }

        void Update()
        {
            if (!IsPlaying)
                return;

            // 没到切换帧的时间
            if ((time += Time.deltaTime) < 1f / frameRate)
                return;
            else
                time = 0f;

            frameIndex++;
            image.sprite = curFrame_List[frameIndex];

            switch (animType)
            {
                case AnimType.In:
                    if (frameIndex >= curFrame_List.Count - 1)
                    {
                        animType = AnimType.Circle;
                        frameIndex = 0;
                        curFrame_List.Clear();
                        circleFrame_List.ForEach(item => curFrame_List.Add(item));
                        break;
                    }
                    break;
                case AnimType.Circle:
                    if (frameIndex >= curFrame_List.Count - 1)
                    {
                        IsPlaying = false;
                        StartCoroutine(AnimPause_IE());
                    }
                    break;
                case AnimType.Out:
                    if (frameIndex >= curFrame_List.Count - 1)
                    {
                        IsPlaying = false;
                    }
                    break;
            }

        }

        IEnumerator AnimPause_IE()
        {
            yield return new WaitForSeconds(loopPauseTime);
            IsPlaying = true;
            frameIndex = 0;
        }

        /// <summary>
        /// 开始播放序列帧
        /// </summary>
        public void PlayFramesAnim()
        {
            // 如果有 InFrame序列帧，则赋值
            if (inFrame_List.Count > 0)
            {
                curFrame_List.Clear();
                inFrame_List.ForEach(item => curFrame_List.Add(item));
                animType = AnimType.In;
            }
            else // 否则 赋值为 CircleFrame序列帧
            {
                curFrame_List.Clear();
                circleFrame_List.ForEach(item => curFrame_List.Add(item));
                animType = AnimType.Circle;
            }

            if (curFrame_List.Count > 0)
            {
                image.sprite = curFrame_List[0];
                frameIndex = 0;
                IsPlaying = true;
            }
            else
            {
                Debug.LogError("当前UI物体" + gameObject.name + "没有初始化或循环序列帧");
            }
        }

        /// <summary>
        /// 结束序列帧播放
        /// </summary>
        public void StopFramesAnim()
        {
            if (outFrame_List.Count > 0)
            {
                curFrame_List.Clear();

                outFrame_List.ForEach(item => curFrame_List.Add(item));
                image.sprite = curFrame_List[0];
                frameIndex = 0;
                animType = AnimType.Out;
                IsPlaying = true;
            }
            else
            {
                IsPlaying = false;
            }
        }
    }
}